package com.zn.web.controller;

import com.zn.entity.AppAgreement;
import com.zn.entity.CustomerMessageRecord;
import com.zn.service.AppAgreementService;
import com.zn.vo.Message;
import com.zn.web.dto.AgreementDto;
import com.zn.web.dto.AppCustomerDto;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author YinJiaWei
 * @version 2019/1/10 10:48
 */
@RestController
@RequestMapping("/agreement")
public class AppAgreementController {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppAgreementController.class);
    @Autowired
    private AppAgreementService appAgreementService;

    /**
     * 获取注册开户协议
     * @return
     */
    @RequestMapping(value = "/getRegisterAgreement",method = RequestMethod.POST)
    public ResponseEntity<Message> getRegisterAgreement() {
        try {
            AppAgreement appAgreement = appAgreementService.getAgreementByAgreementCode("register");
            return ResponseEntity.ok(Message.successData(appAgreement));
        }catch (Exception ex){
            LOGGER.error("getRegisterAgreement........."+ex.getMessage());
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 获取风险提示协议
     * @return
     */
    @RequestMapping(value = "/getRiskTipAgreement",method = RequestMethod.POST)
    public ResponseEntity<Message> getAgreementByAgreementCode(@RequestBody AgreementDto agreementDto) {
        try {
            LOGGER.info("getRiskTipAgreement Request parameters"+agreementDto.toString());
            /**用于判断是否已阅读并确认风险提示协议**/
            if(StringUtils.isBlank(agreementDto.getCustomerId())){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            AppAgreement appAgreement = appAgreementService.getRiskTipAgreement("risktip", agreementDto.getCustomerId());
            return ResponseEntity.ok(Message.successData(appAgreement));
        }catch (Exception ex){
            LOGGER.error("getRiskTipAgreement........."+ex.getMessage());
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 客户协议知晓
     * @return
     */
    @RequestMapping(value = "/customerKnownAgreement",method = RequestMethod.POST)
    public ResponseEntity<Message> customerKnownAgreement(@RequestBody AgreementDto agreementDto) {
        try {
            LOGGER.info("customerKnownAgreement Request parameters------->"+agreementDto.toString());
            /**用户确认已知软件服务协议**/
            if(StringUtils.isBlank(agreementDto.getCustomerId()) || StringUtils.isBlank(agreementDto.getAgreementId())){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            boolean flag = appAgreementService.customerKnownAgreement(agreementDto.getAgreementId(), agreementDto.getCustomerId());
            if(flag){
                return ResponseEntity.ok(Message.REQ_SUCCESS);
            }else{
                return ResponseEntity.ok(Message.SYS_ERROR);
            }
        }catch (Exception ex){
            LOGGER.error("customerKnownAgreement.........",ex.getMessage());
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }

    /**
     * 获取协议列表
     * @return
     */
    @RequestMapping(value = "/getAgreementList",method = RequestMethod.POST)
    public ResponseEntity<Message> getAgreementList(@RequestBody AgreementDto agreementDto) {
        try {
            LOGGER.info("getSoftwareAgreement Request parameters",agreementDto.toString());
            /**用于判断是否已阅读并确认软件服务协议**/
            if(StringUtils.isBlank(agreementDto.getCustomerId())){
                return ResponseEntity.ok(Message.ARGS_ERROR);
            }
            List<AppAgreement> appAgreements = appAgreementService.getAgreementList(agreementDto.getCustomerId());
            return ResponseEntity.ok(Message.successData(appAgreements));
        }catch (Exception ex){
            LOGGER.error("getSoftwareAgreement........."+ex.getMessage());
            return ResponseEntity.ok(Message.SYS_ERROR);
        }
    }






}
